home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / gi / _signalhelper.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  9KB  |  244 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. import sys
  5. import inspect
  6. from _gi import _gobject
  7. if sys.version_info <= sys.version_info:
  8.     pass
  9. elif sys.version_info < (3, 2):
  10.     
  11.     def callable(fn):
  12.         return hasattr(fn, '__call__')
  13.  
  14.  
  15. class Signal(str):
  16.     '''Object which gives a nice API for creating and binding signals.
  17.  
  18.     :param name:
  19.         Name of signal or callable closure when used as a decorator.
  20.     :type name: str or callable
  21.     :param callable func:
  22.         Callable closure method.
  23.     :param GObject.SignalFlags flags:
  24.         Flags specifying when to run closure.
  25.     :param type return_type:
  26.         Return type of the Signal.
  27.     :param list arg_types:
  28.         List of argument types specifying the signals function signature
  29.     :param str doc:
  30.         Documentation of signal object.
  31.     :param callable accumulator:
  32.         Accumulator method with the signature:
  33.         func(ihint, return_accu, handler_return, accu_data) -> boolean
  34.     :param object accu_data:
  35.         User data passed to the accumulator.
  36.  
  37.     :Example:
  38.  
  39.     .. code-block:: python
  40.  
  41.         class Spam(GObject.Object):
  42.             velocity = 0
  43.  
  44.             @GObject.Signal
  45.             def pushed(self):
  46.                 self.velocity += 1
  47.  
  48.             @GObject.Signal(flags=GObject.SignalFlags.RUN_LAST)
  49.             def pulled(self):
  50.                 self.velocity -= 1
  51.  
  52.             stomped = GObject.Signal(\'stomped\', arg_types=(int,))
  53.  
  54.             @GObject.Signal
  55.             def annotated_signal(self, a:int, b:str):
  56.                 "Python3 annotation support for parameter types.
  57.  
  58.         def on_pushed(obj):
  59.             print(obj)
  60.  
  61.         spam = Spam()
  62.         spam.pushed.connect(on_pushed)
  63.         spam.pushed.emit()
  64.     '''
  65.     
  66.     class BoundSignal(str):
  67.         '''
  68.         Temporary binding object which can be used for connecting signals
  69.         without specifying the signal name string to connect.
  70.         '''
  71.         
  72.         def __new__(cls, name, *args, **kargs):
  73.             return str.__new__(cls, name)
  74.  
  75.         
  76.         def __init__(self, signal, gobj):
  77.             str.__init__(self)
  78.             self.signal = signal
  79.             self.gobj = gobj
  80.  
  81.         
  82.         def __repr__(self):
  83.             return 'BoundSignal("%s")' % self
  84.  
  85.         
  86.         def __call__(self, *args, **kargs):
  87.             '''Call the signals closure.'''
  88.             return self.signal.func(self.gobj, *args, **kargs)
  89.  
  90.         
  91.         def connect(self, callback, *args, **kargs):
  92.             '''Same as GObject.Object.connect except there is no need to specify
  93.             the signal name.'''
  94.             return self.gobj.connect(self, callback, *args, **kargs)
  95.  
  96.         
  97.         def connect_detailed(self, callback, detail, *args, **kargs):
  98.             '''Same as GObject.Object.connect except there is no need to specify
  99.             the signal name. In addition concats "::<detail>" to the signal name
  100.             when connecting; for use with notifications like "notify" when a property
  101.             changes.
  102.             '''
  103.             return self.gobj.connect(self + '::' + detail, callback, *args, **kargs)
  104.  
  105.         
  106.         def disconnect(self, handler_id):
  107.             '''Same as GObject.Object.disconnect.'''
  108.             self.instance.disconnect(handler_id)
  109.  
  110.         
  111.         def emit(self, *args, **kargs):
  112.             '''Same as GObject.Object.emit except there is no need to specify
  113.             the signal name.'''
  114.             return self.gobj.emit(str(self), *args, **kargs)
  115.  
  116.  
  117.     
  118.     def __new__(cls, name = '', *args, **kargs):
  119.         if callable(name):
  120.             name = name.__name__
  121.         return str.__new__(cls, name)
  122.  
  123.     
  124.     def __init__(self, name = '', func = None, flags = _gobject.SIGNAL_RUN_FIRST, return_type = None, arg_types = None, doc = '', accumulator = None, accu_data = None):
  125.         if func and not name:
  126.             name = func.__name__
  127.         elif callable(name):
  128.             func = name
  129.             name = func.__name__
  130.         if func and not doc:
  131.             doc = func.__doc__
  132.         str.__init__(self)
  133.         if func:
  134.             if not return_type:
  135.                 pass
  136.             if not arg_types:
  137.                 (return_type, arg_types) = get_signal_annotations(func)
  138.         if arg_types is None:
  139.             arg_types = tuple()
  140.         self.func = func
  141.         self.flags = flags
  142.         self.return_type = return_type
  143.         self.arg_types = arg_types
  144.         self.__doc__ = doc
  145.         self.accumulator = accumulator
  146.         self.accu_data = accu_data
  147.  
  148.     
  149.     def __get__(self, instance, owner = None):
  150.         '''Returns a BoundSignal when accessed on an object instance.'''
  151.         if instance is None:
  152.             return self
  153.         return None.BoundSignal(self, instance)
  154.  
  155.     
  156.     def __call__(self, obj, *args, **kargs):
  157.         '''Allows for instantiated Signals to be used as a decorator or calling
  158.         of the underlying signal method.'''
  159.         if isinstance(obj, _gobject.GObject):
  160.             self.func(obj, *args, **kargs)
  161.         elif str(self):
  162.             name = str(self)
  163.         else:
  164.             name = obj.__name__
  165.         return type(self)(name = name, func = obj, flags = self.flags, return_type = self.return_type, arg_types = self.arg_types, doc = self.__doc__, accumulator = self.accumulator, accu_data = self.accu_data)
  166.  
  167.     
  168.     def copy(self, newName = None):
  169.         '''Returns a renamed copy of the Signal.'''
  170.         if newName is None:
  171.             newName = self.name
  172.         return type(self)(name = newName, func = self.func, flags = self.flags, return_type = self.return_type, arg_types = self.arg_types, doc = self.__doc__, accumulator = self.accumulator, accu_data = self.accu_data)
  173.  
  174.     
  175.     def get_signal_args(self):
  176.         '''Returns a tuple of: (flags, return_type, arg_types, accumulator, accu_data)'''
  177.         return (self.flags, self.return_type, self.arg_types, self.accumulator, self.accu_data)
  178.  
  179.  
  180.  
  181. class SignalOverride(Signal):
  182.     '''Specialized sub-class of Signal which can be used as a decorator for overriding
  183.     existing signals on GObjects.
  184.  
  185.     :Example:
  186.  
  187.     .. code-block:: python
  188.  
  189.         class MyWidget(Gtk.Widget):
  190.             @GObject.SignalOverride
  191.             def configure_event(self):
  192.                 pass
  193.     '''
  194.     
  195.     def get_signal_args(self):
  196.         """Returns the string 'override'."""
  197.         return 'override'
  198.  
  199.  
  200.  
  201. def get_signal_annotations(func):
  202.     """Attempt pulling python 3 function annotations off of 'func' for
  203.     use as a signals type information. Returns an ordered nested tuple
  204.     of (return_type, (arg_type1, arg_type2, ...)). If the given function
  205.     does not have annotations then (None, tuple()) is returned.
  206.     """
  207.     arg_types = tuple()
  208.     return_type = None
  209.     if hasattr(func, '__annotations__'):
  210.         spec = inspect.getfullargspec(func)
  211.         arg_types = (tuple,)((lambda .0: pass)(spec.args))
  212.         if 'return' in spec.annotations:
  213.             return_type = spec.annotations['return']
  214.         
  215.     return (return_type, arg_types)
  216.  
  217.  
  218. def install_signals(cls):
  219.     """Adds Signal instances on a GObject derived class into the '__gsignals__'
  220.     dictionary to be picked up and registered as real GObject signals.
  221.     """
  222.     gsignals = cls.__dict__.get('__gsignals__', { })
  223.     newsignals = { }
  224.     for name, signal in cls.__dict__.items():
  225.         if isinstance(signal, Signal):
  226.             signalName = str(signal)
  227.             if not signalName:
  228.                 signalName = name
  229.                 signal = signal.copy(name)
  230.                 setattr(cls, name, signal)
  231.             if signalName in gsignals:
  232.                 raise ValueError('Signal "%s" has already been registered.' % name)
  233.             newsignals[signalName] = signal
  234.             gsignals[signalName] = signal.get_signal_args()
  235.             continue
  236.     cls.__gsignals__ = gsignals
  237.     for name, signal in newsignals.items():
  238.         if signal.func is not None:
  239.             funcName = 'do_' + name.replace('-', '_')
  240.             if not hasattr(cls, funcName):
  241.                 setattr(cls, funcName, signal.func)
  242.             
  243.  
  244.